package com.zuora.interview.topn.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by chunyang.gao on 2016/8/23.
 */
public class LogGenerator {

    private static final Logger logger = LoggerFactory.getLogger(LogGenerator.class);

    public static void main(String[] args) throws IOException {
        //say there is 1000000 users and 10^2 pages
        Clock clock = Clock.start();
        int userNum = 100000;
        List<String> userNames = genUserNames(userNum);
        List<String> pageNames = genPageNames();
        long total = 10000000l;
        for(long i=0;i<total;i++){
            int randomUserIndex = new Random().nextInt(userNum);
            int randomPageIndex = new Random().nextInt(pageNames.size());
            logger.info(new StringBuilder().append(userNames.get(randomUserIndex)).append(",").append(pageNames.get(randomPageIndex)).toString());
        }
        System.out.println("log generated in "+clock.checkTime()+" ms");
    }

    private static List<String> genPageNames() {
        List<String> rt = new ArrayList();
        String metaChars = "01234567890";
        for(int i=0;i<metaChars.length();i++){
            for(int j=0;j<metaChars.length();j++){
                rt.add(metaChars.substring(i, i + 1) + metaChars.substring(j,j+1));
            }
        }

        return rt;
    }

    private static List<String> genUserNames(int userNum) {
        List<String> rt = new ArrayList();
        for(int i=0;i<userNum;i++){
            rt.add("u"+i);
        }
        return rt;
    }
}
